#include <stdlib.h>

#include "sort.h"

typedef struct {
    STR_NUM **str_num;
    int len;
}RADIX_NUM;

void radix_sort(STR_NUM **nums, int n)
{
    RADIX_NUM **buf = malloc(20 * sizeof(RADIX_NUM *));
    STR_NUM *tmp;
    int flag = 1, i, j, tag, number, count;
    for(i = 0; i < 20; i++){
        buf[i] = malloc(sizeof(RADIX_NUM *));
        buf[i]->str_num = malloc(n * sizeof(STR_NUM*));
    }
    while(flag == 1){
        flag = 0;
        for(i = 0; i < 20; i++){
            buf[i]->len = 0;
        }
        for(i = 0; i < n; i++){
            if(nums[i]->s[0] == '-'){
                tag = 0;
            }else{
                tag = 10;
            }
            if(((nums[i]->s[0]=='-')&&(nums[i]->ptr==0))||(nums[i]->ptr < 0)){
                number = 0;
            }else{
                number = nums[i]->s[nums[i]->ptr--] - '0';
                flag = 1;
            }
            buf[number + tag]->str_num[buf[number + tag]->len++] = nums[i];
        }
        count = 0;
        for(i = 0; i < 20; i++){
            for(j = 0; j < buf[i]->len; j++){
                nums[count++] = buf[i]->str_num[j];
            }
        }
    }
    count = 0;
    for(i = buf[0]->len; i > 0; i--){
        nums[count++] = buf[0]->str_num[i-1];
    }
}

